package dp.danhngon.danhngoncuocsong;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import dp.danhngon.danhngoncuocsong.R;

public class SubListView extends Activity implements android.view.View.OnClickListener {
	ListView listContent;
	ArrayList<Content> array;
	MyAdapter myAdapter;
	String fileName;
	public static final String param= "noidung";

	public  String FILE_CONTENT="";
	String str="";
	Button btCallBack;
	TextView txtAppTitle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
				
		setContentView(R.layout.sub_listview);
		txtAppTitle=(TextView)findViewById(R.id.txtAppTitle);
		
		listContent=(ListView)findViewById(R.id.listSubContent);
		btCallBack=(Button)findViewById(R.id.btCallBack);
		btCallBack.setOnClickListener(this);
		array=new ArrayList<Content>();
				
		Intent myLocalIntent=this.getIntent();
		
		//xem Bundle gui den cac cap du lieu
		Bundle mBundle=myLocalIntent.getExtras();
		fileName=myLocalIntent.getStringExtra("param");
		String strTitle=myLocalIntent.getStringExtra("title");
		txtAppTitle.setText(strTitle);
		//add content vao array
		try{
			ArrayList<String[]> listSubContent=PlayWithRawFile(fileName);
			Content ct;
			for(String[] item:listSubContent){				
				ct=new Content();				
				ct.setContent(item[0]);
				ct.setTitle(item[1]);
				
    			//ct=new Content("Author", item);
				array.add(ct); 	    	    	
    		}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
				
		myAdapter=new MyAdapter(this, R.layout.sub_content, array);
		//set Adapter cho listview
		listContent.setAdapter(myAdapter);
	}
    
	public ArrayList< String[]> PlayWithRawFile(String fileName) throws IOException {
		ArrayList<String[]> listChapter = new ArrayList<String[]>();

		StringBuffer buf = new StringBuffer();
		InputStream is = this.getResources().openRawResource(
				getResources().getIdentifier(fileName, "drawable",
						getPackageName()));
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		// read file
		if (is != null) {
			// Doc tung doan va add vao list
			while ((str = reader.readLine()) != null) {
				//tach phan noi dung va tac gia
			 	String[] arrQuotation=str.split(";");
				listChapter.add(arrQuotation);
			}
		}
		is.close();
		return listChapter;
	}
	
	public void onClick(View v) {
		finish();
		
	}
	
}
